package com.ddpt.platform.common.util;

import cn.hutool.core.util.ReflectUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ddpt.platform.common.base.BizEntity;
import com.ddpt.platform.common.base.DataAuthSessionUser;
import com.ddpt.platform.common.constant.GlobalConstant;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 组织机构处理工具
 */
public class MarketingUtils {

    /**
     * 构建组织机构查询条件
     * @param paramIns 页面传递的组织机构集合
     * @return 最终要查询的组织机构id集合
     */
    public static <T extends BizEntity> LambdaQueryWrapper<T> buildDataAuthQuery(LambdaQueryWrapper<T> wrapper,Collection<Long> paramIns, DataAuthSessionUser sessionUser) {
        if(sessionUser == null){
            return wrapper;
        }
        if(sessionUser.getInsId()==null || CollectionUtils.isEmpty(sessionUser.getDataInsIds())){
            return wrapper;
        }
        if (CollectionUtils.isEmpty(paramIns)){
            paramIns = Collections.singleton(sessionUser.getInsId());
        }
        Integer dataAuth = sessionUser.getDataAuth();
        wrapper.eq(BizEntity::getMerchantId,sessionUser.getMerchantId());
        Class<T> entityClass = wrapper.getEntityClass();
        TableField tableField = ReflectUtil.getField(entityClass, "insId").getDeclaredAnnotation(TableField.class);
        if(tableField==null || !tableField.exist()){
            return wrapper;
        }
        if (sessionUser.getAdminType().equals(GlobalConstant.ONE)) {
            return wrapper.in(CollectionUtils.isNotEmpty(paramIns),BizEntity::getInsId,paramIns);
        }
        //数据权限类型： 0 - 自身创建数据，1 - 当前机构数据权限，2 - 当前机构及其所有内层机构数据权限
        switch (dataAuth) {
            case 0: {
                TableField createUserId = ReflectUtil.getField(entityClass, "createUserId").getDeclaredAnnotation(TableField.class);
                if(createUserId==null || !tableField.exist()){
                    return wrapper;
                }
                return wrapper.eq(BizEntity::getCreateUserId,sessionUser.getUserId());
            }
            case 1: {
                //当前机构
                return wrapper.eq(BizEntity::getInsId,sessionUser.getInsId());
            }
            case 2: {
                //当前及下属机构 12
                if (CollectionUtils.isEmpty(paramIns)) {
                    return wrapper.eq(BizEntity::getInsId,sessionUser.getInsId());
                }
                List<Long> dataIns = sessionUser.getDataInsIds();
                Set<Long> result = paramIns.stream().filter(dataIns::contains).collect(Collectors.toSet());
                if (CollectionUtils.isEmpty(result)) {
                    return wrapper.eq(BizEntity::getInsId,sessionUser.getInsId());
                }
                return wrapper.in(BizEntity::getInsId,result);
            }
            default:{
                return wrapper;
            }
        }
    }

    public static <T extends BizEntity> QueryWrapper<T> buildDataAuthQuery(QueryWrapper<T> wrapper, Collection<Long> paramIns, DataAuthSessionUser sessionUser) {
        if(sessionUser == null){
            return wrapper;
        }
        Integer dataAuth = sessionUser.getDataAuth();
        if(sessionUser.getInsId()==null || CollectionUtils.isEmpty(sessionUser.getDataInsIds())){
            return wrapper;
        }
        wrapper.eq("merchant_id",sessionUser.getMerchantId());
        Class<T> entityClass = wrapper.getEntityClass();
        TableField tableField = ReflectUtil.getField(entityClass, "insId").getDeclaredAnnotation(TableField.class);
        if(tableField==null || !tableField.exist()){
            return wrapper;
        }
        if (sessionUser.getAdminType().equals(GlobalConstant.ONE)) {
            return wrapper.in(CollectionUtils.isNotEmpty(paramIns),"ins_id",paramIns);
        }
        //数据权限类型： 0 - 自身创建数据，1 - 当前机构数据权限，2 - 当前机构及其所有内层机构数据权限
        switch (dataAuth) {
            case 0: {
                TableField createUserId = ReflectUtil.getField(entityClass, "createUserId").getDeclaredAnnotation(TableField.class);
                if(createUserId==null || !tableField.exist()){
                    return wrapper;
                }
                return wrapper.eq("create_user_id",sessionUser.getUserId());
            }
            case 1: {
                //当前机构
                return wrapper.eq("ins_id",sessionUser.getInsId());
            }
            case 2: {
                //当前及下属机构
                if (CollectionUtils.isEmpty(paramIns)) {
                    return wrapper.eq("ins_id",sessionUser.getInsId());
                }
                List<Long> dataIns = sessionUser.getDataInsIds();
                Set<Long> result = paramIns.stream().filter(dataIns::contains).collect(Collectors.toSet());
                if (CollectionUtils.isEmpty(result)) {
                    return wrapper.eq("ins_id",sessionUser.getInsId());
                }
                return wrapper.in("ins_id",result);
            }
            default:{
                return wrapper;
            }
        }
    }

}
